package com.simple.datax.db.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.simple.datax.core.model.connpara.CommonConnPara;
import com.simple.datax.db.mybatis.client.DataxReceiveChannelMapper;
import com.simple.datax.db.mybatis.client.DataxSelfnodePropMapper;
import com.simple.datax.db.mybatis.model.DataxReceiveChannel;
import com.simple.datax.db.mybatis.model.DataxReceiveChannelCriteria;
import com.simple.datax.db.mybatis.model.DataxReceiveChannelCriteria.Criteria;
import com.simple.datax.db.mybatis.model.DataxSelfnodeProp;
import com.simple.datax.db.mybatis.model.DataxSelfnodePropCriteria;
import com.simple.datax.service.DBLoaderService;

/**
 * @Title: DBLoaderServiceImpl.java
 * @Package com.simple.datax.db.service.impl
 * @Description: 数据库加载接收
 * @author Paladin Chen
 * @date 2012-12-24 下午5:39:24
 * @version V1.0
 */
public class DBLoaderServiceImpl implements DBLoaderService {
	private static final Logger logger = LoggerFactory.getLogger(DBLoaderServiceImpl.class);
	private List<String> communTypes;

	private List<String> excludeCommunTypes;

	private Short group = 1;

	// 只有节点间的通道，如果是，则只读取节点间的通道参数，其他参数都没有作用。
	private boolean onlyNodeChannel = false;

	// 是不是包括节点间的通道，
	private boolean includeNodeChannel = true;

	@Autowired
	private DataxReceiveChannelMapper mrcMapper;

	@Autowired
	private DataxSelfnodePropMapper dsnpMapper;

	@Override
	public List<CommonConnPara> getRecvConnParas() {
		logger.debug("Begin to get Receive Connector Info....");
		long start = System.currentTimeMillis();
		List<CommonConnPara> result = null;

		// 处理非只有节点间通道参数的过程，即根据参数，确定取出企业通道参数（或包含节点间通道参数）
		if (!onlyNodeChannel) {
			DataxReceiveChannelCriteria example = new DataxReceiveChannelCriteria();
			Criteria ct = example.createCriteria();

			ct.andGroupEqualTo(group).andFlagEqualTo((short) 1);

			// 要支持多个的配置
			if (communTypes != null && communTypes.size() > 0) {
				ct.andCommunTypeIn(communTypes);
			}

			if (excludeCommunTypes != null && excludeCommunTypes.size() > 0) {
				ct.andCommunTypeNotIn(excludeCommunTypes);
			}

			List<DataxReceiveChannel> mrcs = mrcMapper.selectByExample(example);

			if (mrcs != null) {
				result = new ArrayList<CommonConnPara>();
				for (DataxReceiveChannel mrc : mrcs) {
					CommonConnPara ccp = new CommonConnPara();

					ccp.setEncoding(mrc.getMsgCharset());
					ccp.setMsgFormat(mrc.getMsgFormat());
					ccp.setMsgType(mrc.getMsgType());
					ccp.setTransportPara(mrc.getCommunParam());
					ccp.setTransportType(mrc.getCommunType());

					result.add(ccp);
				}
			}
			
			// 是否包含节点间通道参数
			if(includeNodeChannel){
				List<CommonConnPara> resultNode = getNodeRecvConnParas();
				
				if(resultNode != null){
					if(result != null){
						result.addAll(resultNode);
					} else {
						result = resultNode;
					}
				}
			}
		} else {
			result = getNodeRecvConnParas();
		}

		if (result != null) {
			logger.debug("End get Receive Connector Info...." + result.size());
		} else {
			logger.debug("End get Receive Connector Info.... Null");
		}
		long end = System.currentTimeMillis();
		logger.debug("======== Load recv time:" + (end-start));
		return result;
	}

	//读取节点间的通道
	private List<CommonConnPara> getNodeRecvConnParas() {
		List<CommonConnPara> result = null;
		DataxSelfnodePropCriteria example = new DataxSelfnodePropCriteria();

		List<DataxSelfnodeProp> dsnps = dsnpMapper.selectByExample(example);

		if (dsnps != null) {
			result = new ArrayList<CommonConnPara>();
			for (DataxSelfnodeProp dsnp : dsnps) {
				CommonConnPara ccp = new CommonConnPara();

				ccp.setEncoding(dsnp.getMsgCharset());
				ccp.setMsgFormat(dsnp.getMsgFormat());
				ccp.setMsgType(dsnp.getMsgType());
				ccp.setTransportPara(dsnp.getCommunRecvParam());
				ccp.setTransportType(dsnp.getCommunRecvType());

				result.add(ccp);
			}
		}

		return result;
	}

	public int getGroup() {
		return group;
	}

	public void setGroup(Short group) {
		this.group = group;
	}

	public List<String> getCommunTypes() {
		return communTypes;
	}

	public void setCommunTypes(List<String> communTypes) {
		this.communTypes = communTypes;
	}

	public List<String> getExcludeCommunTypes() {
		return excludeCommunTypes;
	}

	public void setExcludeCommunTypes(List<String> excludeCommunTypes) {
		this.excludeCommunTypes = excludeCommunTypes;
	}

	public boolean isOnlyNodeChannel() {
		return onlyNodeChannel;
	}

	public void setOnlyNodeChannel(boolean onlyNodeChannel) {
		this.onlyNodeChannel = onlyNodeChannel;
	}

	public boolean isIncludeNodeChannel() {
		return includeNodeChannel;
	}

	public void setIncludeNodeChannel(boolean includeNodeChannel) {
		this.includeNodeChannel = includeNodeChannel;
	}

}
